//
//  ViewController.m
//  CustomCamera
//
//  Created by SuJiang on 2018/4/2.
//  Copyright © 2018年 appscomm. All rights reserved.
//

#import "ViewController.h"
#import "LYCameraPreviewView.h"

@interface ViewController ()<LYCameraManagerDelegate>

@property(nonatomic, strong) LYCameraManager *cameraManager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cameraManager = [[LYCameraManager alloc] initWithMode:LYCameraCaptureModeVideo];
    self.cameraManager.delegate = self;
    self.cameraManager.previewView.frame = self.view.bounds;
    [self.view addSubview:self.cameraManager.previewView];
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.cameraManager startSessionRunning];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.cameraManager stopSessionRunning];
    [super viewDidDisappear:animated];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    
    if ( UIDeviceOrientationIsPortrait( deviceOrientation ) || UIDeviceOrientationIsLandscape( deviceOrientation ) ) {
        AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.cameraManager.previewView.layer;
        previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation)deviceOrientation;
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

// 对焦
- (void) switchFocusMode:(AVCaptureFocusMode)mode
{
    [self.cameraManager switchFocusMode:mode];
}

- (void) setFocusLensPosition:(CGFloat)position
{
    [self.cameraManager setFocusLensPosition:position];
}

#pragma mark - 曝光
- (void) setExposureTargetBias:(CGFloat)bias
{
    [self.cameraManager setExposureTargetBias:bias];
}

#pragma mark - delegate
- (void) cameraSessionSetupError:(LYCameraSetupResult)result
{
    
}

- (void) cameraSessionResumeFailed
{
    
}

- (void) cameraSessionVideoFrame:(CGImageRef)frame
{
    
}


@end
